package cn.xxxy.fourm.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xxxy.fourm.dao.UserDao;
import cn.xxxy.fourm.entity.User;
import cn.xxxy.fourm.entity.UserRole;
import cn.xxxy.fourm.entity.UserState;
import cn.xxxy.fourm.model.DataGrid;
import cn.xxxy.fourm.model.Message;
import cn.xxxy.fourm.model.UserDto;
import cn.xxxy.fourm.model.UserRoleDto;
import cn.xxxy.fourm.model.UserStateDto;
import cn.xxxy.fourm.service.UserRoleService;
import cn.xxxy.fourm.service.UserService;
import cn.xxxy.fourm.service.UserStateService;
import cn.xxxy.fourm.util.StringUtil;

@Controller
public class UserController {
	private Logger logger=LoggerFactory.getLogger("cn.xxxy.fourm.Controller");
	@Autowired
	private UserService userService;
	@Autowired
	private UserStateService userStateService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private UserDao userDao;
	/**
	 * 1.显示
	 * @return
	 */
	@RequestMapping("/listUsers")
	public String lists(Model m) {
		return "/user/users";
	}
	/**
     * 2、显示列表数据
     * @param page
     * @param rows
     * @return
     */ 
	@RequestMapping("/list1")
	@ResponseBody
	public DataGrid<UserDto> list1(int page,int rows) {
		List<UserDto> lists=userService.listPage(page,rows);
		Long count = userDao.count(); 
		DataGrid<UserDto> datagrid=new DataGrid<UserDto>();
		datagrid.setRows(lists);
		datagrid.setTotal(count);
		datagrid.setSuccess(true);
		return datagrid;
	}
	/**
     * 3.添加操作
     * @param user
     * @return
     */
	@PostMapping("/user/save")
	@ResponseBody
	public Message saveUser(User user,int rid,int sid) {
		Message message=new Message();
		UserState userState =new UserState();
		userState.setSid(sid);
		UserRole userRole=new UserRole();
		userRole.setRid(rid);
		user.setUserState(userState);
		user.setUserRole(userRole);
		User user1=userDao.saveAndFlush(user);
		if(user1!=null) {
			message.setMsg("恭喜您，添加成功");
			message.setSuccess(true);
		}else {
		message.setMsg("很遗憾，添加失败");
		message.setSuccess(false);
		}
		return message;
	}
	/**
     * 4删除操作
     * @param ids
     * @return
     */ 
	@ResponseBody
	@RequestMapping("/user/delete")
	public Message delete(int uid) {
		Message message=new Message();
		userDao.deleteById(uid);
		User user1=userDao.getOne(uid);
		if(user1!=null) {
			message.setMsg("恭喜您，删除成功");
			message.setSuccess(true);
		}else {
			message.setMsg("很遗憾，删除失败");
			message.setSuccess(false);
		}
		return message;
	}
	@ResponseBody 
    @RequestMapping("/user/deletes") 
	public Map<String, Object> delete(@RequestParam(value = "ids") String ids) { 
		Map<String, Object> resultMap = new HashMap<String, Object>(); 
		String[] idsStr = ids.split(","); 
		for (int i = 0; i < idsStr.length; i++) { 
			userDao.deleteById(Integer.parseInt(idsStr[i]));
			} resultMap.put("success", true); 
			return resultMap; 
		}
	@ResponseBody 
	@RequestMapping("/user/roles") 
	public List<UserRoleDto> roles(){
		List<UserRoleDto> lists=userRoleService.listUserRoles();
		return lists;		
	}
	@ResponseBody 
	@RequestMapping("/user/states") 
	public List<UserStateDto> states(){
		List<UserStateDto> lists=userStateService.listUserStates();
		return lists;		
	}
	
	@RequestMapping("/user/update")
	@ResponseBody
	public Message updateUser(User user,int rid,int sid,int uid) {
		Message message=new Message();
		UserState userState =new UserState();
		userState.setSid(sid);
		UserRole userRole=new UserRole();
		userRole.setRid(rid);
		user.setUserState(userState);
		user.setUserRole(userRole);
		user.setUid(uid);
		User user1=userDao.saveAndFlush(user);
		//logger.info("user"+user1.getUname()+"更新用户");
		if(user1!=null) {
			message.setMsg("恭喜您，更新成功");
			message.setSuccess(true);
		}else {
			message.setMsg("很遗憾，更新失败");
			message.setSuccess(false);
		}
		return message;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
